
// global variable holder
// so i won't pollute the browser/page namespace'
var game={};
game.debug=0;

var GameObject = new Class({
	initialize: function(){
		this.x=0;
		this.y=0;
		this.z=0;
		this.HTMLIncarnation=null;
                this.world=null;
                this.type=null;
	},
        comeIntoExistence: function(world){
            this.HTMLIncarnation.injectInside(world.HTMLIncarnation);
            this.world=world;
        }
});

var World = new Class({
        Extends: GameObject,
	initialize: function(width,height){
		this.parent();
		this.HTMLIncarnation = new Element('div',{'class': 'world'});
                this.width = width;
                this.height = height;
                this.type='world';
                this.worldWindow={width: 0,height: 0};
                this.scrollStep=35;
                this.viewport=$('viewport');
                this.verticalMove = new Fx.Tween(this.HTMLIncarnation, {duration: 250});
                this.horizontalMove = new Fx.Tween(this.HTMLIncarnation, {duration: 250});
	},
	start: function(){
            this.scrollOffset=0;
          this.HTMLIncarnation.injectInside(this.viewport);
          this.worldWindow.width=parseInt(this.viewport.getStyle('width'));
          this.worldWindow.height=parseInt(this.viewport.getStyle('height'));
          this.HTMLIncarnation.setStyle('width',this.width);
          this.HTMLIncarnation.setStyle('height',this.height);
    },
    changeWorldParameters: function(width,height){
        this.width = width;
        this.height = height;
        this.HTMLIncarnation.setStyle('width',this.width);
        this.HTMLIncarnation.setStyle('height',this.height);
    },
    moveLeft: function(){
        var step=this.scrollStep;
        if(this.x-step+game.worldWidth<=this.worldWindow.width)
            step=this.x+game.worldWidth-this.worldWindow.width;
        if(step>0){
          this.horizontalMove.start('left',this.x,this.x-step);
          this.x=this.x-step;
        }
    },
    moveRight: function(){
        var step=this.scrollStep;
        if(this.x+step>0)
            step=(-1)*this.x;
        if(step>0){
          this.horizontalMove.start('left',this.x,this.x+step);
          this.x=this.x+step;
        }
    },
    moveUp: function(){
        var step=this.scrollStep;
        if(this.y-step+game.worldHeight<=this.worldWindow.height)
            step=this.y+game.worldHeight-this.worldWindow.height;
        if(step>0){
          this.verticalMove.start('top',this.y,this.y-step);
          this.y=this.y-step;
        }
    },
    moveDown: function(){
        var step=this.scrollStep;
        if(this.y+step>0)
            step=(-1)*this.y;
        if(step>0){
          this.verticalMove.start('top',this.y,this.y+step);
          this.y=this.y+step;
        }
    },
    resetIntoView: function(){
        if(this.x!=0){
            this.horizontalMove.set('left',0);
            this.x=0;
        }
        if(this.y!=0){
            this.verticalMove.set('top',0);
            this.y=0;
        }
    },
    insertObjectIntoWorld: function(obiect){
        obiect.comeIntoExistence(this);
    },
    getWorldWrapper: function(){
        return this.viewport;
    }
});

window.addEvent('domready', startGame);

function initWorld(){
    initGame();
    loopSound('bgsound');
    if(game.debug==1){
        game.debugMessage = '';
        game.debugConsole = new Element('div',{'class': 'debug'});
        game.debugConsole.injectInside(document.body);
        game.appendLog = function(mesaj){
            game.debugMessage+='<br>'+mesaj;
        };
        game.writeLog=function(){
            this.debugConsole.set('html',this.debugMessage);
            this.debugMessage = '';
        };
        game.world.HTMLIncarnation.addEvent('click',function(e){
                                          var zone=util.getZoneAtPoint(e.event.clientX-150,e.event.clientY-20);
                                          game.clickedZone = zone;
                                          });
    }
}

function startGame() {
    var introDiv = new Element('div',{'class': 'intro'});
    introDiv.set('html','<br>JS BOMBERMAN<BR>BACK IN GLORY<br>Arrows to move the hero and SPACE to launch a bomb.<br><br><br>');
    var startButton = new Element('a',{'href': '#top','events': {'click': function(){
                                                      //loadImages();
                                                      var introEffect = new Fx.Tween(this,{'duration': 500});
                                                      introEffect.addEvent('onComplete',function(){this.destroy();initWorld();}.bind(this));
                                                      introEffect.start('opacity',1,0);
                                                    }.bind(introDiv)}});
    startButton.set('text','START');
    var startDebugButton = new Element('a',{'href': '#top','events': {'click': function(){
                                                      //loadImages();
                                                      var introEffect = new Fx.Tween(this,{'duration': 500});
                                                      introEffect.addEvent('onComplete',function(){game.debug=1;this.destroy();initWorld();}.bind(this));
                                                      introEffect.start('opacity',1,0);
                                                    }.bind(introDiv)}});
    startButton.set('text','START');
    startButton.inject(introDiv);
    var br=new Element('br');
    br.inject(introDiv);
    br.clone(false).inject(introDiv);
    startDebugButton.set('text','START-DEBUGMODE');
    startDebugButton.inject(introDiv);
    
    introDiv.inject($('viewport'));
    $('bgsoundcheckbox').addEvent('click',function(){
    game.bgsound=game.bgsound ? false : true;
    if(game.bgsound){
        loopSound('bgsound');
        this.blur();
    }
    else {
        soundManager.stop('bgsound');
        $clear(game.bgsoundflag);
        this.blur();
    }
    });
}

// used for debugging purposes . it adds to the window.status the x,y : where the mouse points on the screen
function mouseHoverProcessor(e){
    //var status = window.status.split(' --- ')[0];
    //window.status=status+' --- '+e.clientX+' '+e.clientY;
}

soundManager.debugMode = false;
soundManager.onload = function() {
  soundManager.createSound({id: 'bomb3',url: 'sounds/bomb4.mp3'});
  soundManager.createSound({id: 'bgsound',url: 'sounds/bgsound.mp3',autoLoad: true,volume: 40});
}

if(game.debug==1)
  document.onmousemove=mouseHoverProcessor;

/**
 * loadImages
 */
/*
function loadImages() {
    game.images = {};
    game.images.heroStop=Asset.image('img/player-walking-stop.gif');
    game.images.heroWalkingUp=Asset.image('img/player-walking-back.gif');
    game.images.heroWalkingDown=Asset.image('img/player-walking-front.gif');
    game.images.heroWalkingLeft=Asset.image('img/player-walking-left.gif');
    game.images.heroWalkingRight=Asset.image('img/player-walking-right.gif');
} */

function loopSound(soundID) {
  game.bgsoundflag = window.setTimeout(function() {
      if(game.bgsound)
        soundManager.play(soundID,{onfinish:function(){loopSound(soundID);}});
  },1);
}
